package org.unique.generator;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.filechooser.FileSystemView;

import org.apache.log4j.Logger;
import org.unique.generator.model.Database;
import org.unique.generator.model.Table;

/**
 * 代码生成类
 * 
 * @ClassName: DomainGenerator
 * @author AndrewWen
 * @date 2013-1-14 下午8:45:30
 */
public class CodeGenerator {

	private Logger logger = Logger.getLogger(CodeGenerator.class);
	
	//是否去掉模块前面的编号
	public static String prefix = "";
	
	public boolean generator(final String classDriver, final  String url, final String username, final String password, 
			final String classPackage, final String author, final String contact, final String codePath,
			final String description) {

		String sourcePath = codePath + File.separator + "src/";
		Long start = System.currentTimeMillis();
		try {
			// 获取数据库名
			String schema = url.substring(url.lastIndexOf("/") + 1);
			
			//获取当前用户桌面路径
			FileSystemView fsv=FileSystemView.getFileSystemView();
		    String path=fsv.getHomeDirectory().toString();
		    logger.info(path);
		    
			// 获取数据库信息
			Database databaseBean = new DatabaseInfoOp(classDriver, url, username, password, schema).getDbInfo();
			
			// 获取该库所有表
			List<Table> tableList = databaseBean.getTableList();
			
			logger.info("---------------start---------------");
			
			/**
			 * 遍历生成代码
			 */
			for (Table table : tableList) {

				table.setPackageName(classPackage);
				
				Map<String, Object> map = new HashMap<String, Object>();

				map.put("table", table);
				map.put("contact", contact);
				map.put("author", author);
				map.put("description", description);
				map.put("date", StringUtil.date2String());

				String type="upingan";
				//type="cms";
				type="ojobs";
				if("upingan".equals(type)){
					VelocityInfoOp.generatorCode("model.vm", map, sourcePath + table.getPackagePath() + "/dto", table.getClassName() + "DTO.java");
					VelocityInfoOp.generatorCode("xml.vm", map, sourcePath + table.getPackagePath() + "/dao/sql", table.getClassName() + ".xml");
					VelocityInfoOp.generatorCode("dao.vm", map, sourcePath + table.getPackagePath() + "/dao", table.getClassName() + "Dao.java");
					VelocityInfoOp.generatorCode("daoImpl.vm", map, sourcePath + table.getPackagePath() + "/dao/impl", table.getClassName() + "DaoImpl.java");
					VelocityInfoOp.generatorCode("service.vm", map, sourcePath + table.getPackagePath() + "/service", table.getClassName() + "Service.java");
					VelocityInfoOp.generatorCode("serviceImpl.vm", map, sourcePath + table.getPackagePath() + "/service/impl", table.getClassName() + "ServiceImpl.java");
					VelocityInfoOp.generatorCode("controller.vm", map, sourcePath + table.getPackagePath(), table.getClassName() + "Controller.java");
					VelocityInfoOp.generatorCode("jsp.vm", map, sourcePath + table.getPackagePath(), table.getClassNameFirstLower() + ".jsp");
				}else if("cms".equals(type)){
					VelocityInfoOp.generatorCode("/cms/model.vm", map, sourcePath + table.getPackagePath() + "/domain", table.getClassName() + ".java");
					VelocityInfoOp.generatorCode("/cms/dao.vm", map, sourcePath + table.getPackagePath() + "/dao", table.getClassName() + "Dao.java");
					VelocityInfoOp.generatorCode("/cms/daoImpl.vm", map, sourcePath + table.getPackagePath() + "/dao", table.getClassName() + "DaoImpl.java");
					VelocityInfoOp.generatorCode("/cms/service.vm", map, sourcePath + table.getPackagePath() + "/service", table.getClassName() + "Service.java");
					VelocityInfoOp.generatorCode("/cms/serviceImpl.vm", map, sourcePath + table.getPackagePath() + "/service", table.getClassName() + "ServiceImpl.java");

				}else if("ojobs".equals(type)){
					VelocityInfoOp.generatorCode("/ojobs/model.vm", map, sourcePath + table.getPackagePath() + "/model", table.getClassName() + ".java");
					VelocityInfoOp.generatorCode("/ojobs/controller.vm", map, sourcePath + table.getPackagePath(), table.getClassName() + "Controller.java");
					VelocityInfoOp.generatorCode("/ojobs/list.vm", map, sourcePath + table.getPackagePath()+"/"+table.getClassNameFirstLower() ,  "list.html");
					VelocityInfoOp.generatorCode("/ojobs/form.vm", map, sourcePath + table.getPackagePath()+"/"+table.getClassNameFirstLower() ,"form.html");
				}
				
				logger.info("表：" + table.getTableName() + "成功");
			}

			logger.info("--------------- end time：" + (System.currentTimeMillis() - start) + "ms-----");
			logger.info("代码路径：" + codePath);
			logger.info("包：" + classPackage);
			logger.info("作者：" + author);
			logger.info("联系：" + contact);
			logger.info("------------------------------------");
			return true;
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return false;
	}
	
}
